﻿using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;

namespace Oliving.Edge.Model.Models
{
    public class RootEntity
    {
        /// <summary>
        /// ID
        /// </summary>
        [SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }





    }


    public class BaseEntity
    {
        #region  排序、删除、有效  字段

        /// <summary> 
        /// Desc:排序码--------------------------- 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "SortCode", ColumnDescription = "排序码", IsNullable = true)]
        public int SortCode { get; set; }

        /// <summary> 
        /// Desc:删除标记 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "DeleteMark", ColumnDescription = "删除标记", IsNullable = true)]
        public int DeleteMark { get; set; }

        /// <summary> 
        /// Desc:有效标志 是否启用 0未启用，1启用
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "EnabledMark", ColumnDescription = "有效标志", IsNullable = true)]
        public int EnabledMark { get; set; }


        #endregion

        #region 记录数据的时间的操作者

        /// <summary> 
        /// Desc:创建日期 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "CreateDate", ColumnDescription = "创建日期", IsNullable = true)]
        public DateTime CreateDate { get; set; }

        /// <summary> 
        /// Desc:创建用户主键 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "CreateUserId", ColumnDescription = "创建用户主键", IsNullable = true, Length = 50, ColumnDataType = "nvarchar")]
        public string CreateUserId { get; set; }

        /// <summary> 
        /// Desc:创建用户 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "CreateUserName", ColumnDescription = "创建用户", IsNullable = true, Length = 50, ColumnDataType = "nvarchar")]
        public string CreateUserName { get; set; }

        /// <summary> 
        /// Desc:修改日期 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "ModifyDate", ColumnDescription = "修改日期", IsNullable = true)]
        public DateTime ModifyDate { get; set; }

        /// <summary> 
        /// Desc:修改用户主键 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "ModifyUserId", ColumnDescription = "修改用户主键", IsNullable = true, Length = 50, ColumnDataType = "nvarchar")]
        public string ModifyUserId { get; set; }

        /// <summary> 
        /// Desc:修改用户 
        /// Default: 
        /// Nullable:True 
        /// </summary> 

        [SugarColumn(ColumnName = "ModifyUserName", ColumnDescription = "修改用户", IsNullable = true, Length = 50, ColumnDataType = "nvarchar")]
        public string ModifyUserName { get; set; }
        #endregion

        #region  创建和修改， 记录用户信息

        public virtual void Create(IHttpContextAccessor accessor)
        {

            if (accessor != null && accessor.HttpContext != null)
            {

                var httpContext = accessor.HttpContext;



                string userID = (from item in httpContext.User.Claims
                                 where item.Type == JwtRegisteredClaimNames.Jti
                                 select item.Value).FirstOrDefault();

                string userName = (from item in httpContext.User.Claims
                                   where item.Type == ClaimTypes.Name
                                   select item.Value).FirstOrDefault();


                this.CreateDate = DateTime.Now;
                this.CreateUserId = userID;
                this.CreateUserName = userName;
            }

        }

        public virtual void Modify(IHttpContextAccessor accessor, string KeyValue)
        {
            if (accessor != null && accessor.HttpContext != null)
            {

                var httpContext = accessor.HttpContext;



                string userID = (from item in httpContext.User.Claims
                                 where item.Type == JwtRegisteredClaimNames.Jti
                                 select item.Value).FirstOrDefault();

                string userName = (from item in httpContext.User.Claims
                                   where item.Type == ClaimTypes.Name
                                   select item.Value).FirstOrDefault();


                this.ModifyDate = DateTime.Now;
                this.ModifyUserId = userID;
                this.ModifyUserName = userName;
            }

        }

        #endregion

    }





}